package com.enjoy.mall.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.enjoy.mall.sentinel.entity.UserEntity;
import com.enjoy.mall.sentinel.service.UserService;
import com.tuling.mall.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserService userService;


//    @RequestMapping(value = "/findOrderByUserId/{id}")
//    @SentinelResource(value = "findOrderByUserId",
//            blockHandler = "handleException")
//    public R findOrderByUserId(@PathVariable("id") Integer id) {

//        try {
//            // 模拟测试并发线程数限流
//            Thread.sleep(900);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    //feign调用
//        return orderFeignService.findOrderByUserId(id);

//    }

    public R handleException(@PathVariable("id") Integer id, BlockException exception) {
        System.out.println(id);

        return R.error(-1, "===被限流降级啦===");
    }

    public R fallback(@PathVariable("id") Integer id, Throwable e) {
        return R.error(-1, "===被熔断降级啦===" + e.getMessage());
    }


    @RequestMapping("/info/{id}")
    @SentinelResource(value = "userInfo", blockHandler = "handleException")
    public R info(@PathVariable("id") Integer id) {
        UserEntity user = userService.getById(id);

        if (id == 4) {
            throw new IllegalArgumentException("异常参数");
        }

        return R.success();
    }
}
